package com.gccloud.design;


import com.gccloud.design.dto.PriRelDto;
import com.gccloud.design.dto.SSschemeSaveDto;
import com.gccloud.design.dto.SSschemeUpdateDto;
import com.gccloud.design.service.SdSchPriRelSevice;
import com.gccloud.design.service.SdSchemeService;
import com.gccloud.design.vo.PriRelVO;
import com.gccloud.design.vo.SdSchemeVO;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author mengyu
 * @date 2020/12/26 10:27
 */
@Api(tags = "调度方案模块")
@Slf4j
@RestController
@RequestMapping("/scheme")
public class SdSchemeController {

    @Resource
    private SdSchemeService sdSchemeService;
    @Resource
    private SdSchPriRelSevice sdSchPriRelSevice;

    @RequestMapping(value = "/getScheme", method = RequestMethod.POST)
    @ApiOperation(value = "查询调度方案")
    public R<PageVO<SdSchemeVO>> getAllScheme(@RequestBody SearchDTO searchDTO) {
        log.info("查询调度方法列表：参数：searchDTO+" + searchDTO);
        PageVO<SdSchemeVO> schemeList = sdSchemeService.getAllScheme(searchDTO);
        PageVO pageVO = BeanConvertUtils.convert(schemeList, PageVO.class);
        pageVO.setList(schemeList.getList());
        return R.success(pageVO);

    }

    @RequestMapping(value = "/addScheme", method = RequestMethod.POST)
    @ApiOperation(value = "增加调度方案")
    public R<String> addScheme(@RequestBody SSschemeSaveDto sSschemeSaveDto) {
        sdSchemeService.saveScheme(sSschemeSaveDto);
        return R.success("成功");
    }

    @RequestMapping(value = "/updateScheme", method = RequestMethod.POST)
    @ApiOperation(value = "修改调度方案")
    public R<String> updateScheme(@RequestBody SSschemeUpdateDto sSschemeUpdateDto) {
        sdSchemeService.updateScheme(sSschemeUpdateDto);
        return R.success("成功");
    }

    @RequestMapping(value = "/delSchAndPriRel", method = RequestMethod.GET)
    @ApiOperation(value = "删除调度方案")
    public R<String> deleteSchemeAndPriRel(String schemeId) {
        if (StringUtils.isEmpty(schemeId)) {
            throw new GlobalException("请求入参有误");
        } else {
            sdSchPriRelSevice.deleteSchemeAndPriRel(Integer.parseInt(schemeId));
            return R.success("成功");
        }
    }


    @RequestMapping(value = "/getPriRelList", method = RequestMethod.POST)
    @ApiOperation(value = "查询调度方案关联的调度原则(分页)")
    public R<Map<String, Object>> getPriRelList(@RequestBody SearchDTO searchDTO) {
        log.info("依据 schemeId 查询调度方案关联的调度原则(分页)");
        return R.success(sdSchPriRelSevice.getPriRelList(searchDTO));
    }

    @RequestMapping(value = "/getPriRel", method = RequestMethod.GET)
    @ApiOperation(value = "查询调度方案关联的调度原则（不分页）")
    public R<List<PriRelVO>> getPriRel(String schemeId) {
        if (schemeId == null || "".equals(schemeId) || Integer.parseInt(schemeId) < 0) {
            throw new GlobalException("请求入参有误");
        } else {
            return R.success(sdSchPriRelSevice.getPriRel(Integer.parseInt(schemeId)));
        }
    }

    @GetMapping(value = "/getPriRel/{schemeId}")
    @ApiOperation(value = "查询调度方案关联的调度原则及原则属性")
    public R<List<PriRelDto>> getPriInfo(@PathVariable(value = "schemeId") String schemeId) {
        if (schemeId == null || "".equals(schemeId) || Integer.parseInt(schemeId) < 0) {
            throw new GlobalException("请求入参有误");
        } else {
            return R.success(sdSchPriRelSevice.getPriRelAttr(schemeId));
        }
    }
}
